using Microsoft.Extensions.Logging;
using UniversalAdminSystem.Application.FileStorage.DTOs;
using UniversalAdminSystem.Application.FileStorage.Interfaces;
using UniversalAdminSystem.Infrastructure.Persistence.DbContexts;
using UniversalAdminSystem.Infrastructure.RabbitMQ.Jobs;

namespace UniversalAdminSystem.Infrastructure.RabbitMQ.Queues;

public class EfFileProcessingQueue : IFileProcessingQueue
{
    private readonly UniversalAdminSystemDbContext _dbContext;
    private readonly ILogger<EfFileProcessingQueue> _logger;

    public EfFileProcessingQueue(UniversalAdminSystemDbContext dbContext, ILogger<EfFileProcessingQueue> logger)
    {
        _dbContext = dbContext;
        _logger = logger;
    }

    public async Task EnqueueAsync(FileProcessingJobDto jd, CancellationToken ct = default)
    {
        try
        {
            _logger.LogInformation("准备插入任务: {FileId}, FilePath: {FilePath}", jd.FileId, jd.FilePath);
            _dbContext.FileProcessingJobs.Add(new FileProcessingJob
            {
                FileId = jd.FileId,
                FilePath = jd.FilePath,
                ContentType = jd.ContentType,
                Size = jd.Size
            });
            _logger.LogInformation("DbContext IsConnected: {IsConnected}", _dbContext.Database.CanConnect());
            await _dbContext.SaveChangesAsync(ct);
            _logger.LogInformation("文件处理任务已入队: {FileId}", jd.FileId);
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "文件处理任务入队失败: {FileId}", jd.FileId);
            throw;
        }
    }
}
// public class EfFileProcessingQueue : IFileProcessingQueue
// {
//     public Task EnqueueAsync(FileProcessingJobDto jd, CancellationToken ct = default)
//     {
//         // 什么都不做，直接返回
//         return Task.CompletedTask;
//     }
// }